package com.net.wanjian.networkhospitalmanager.activity.my;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.LoginActivity;
import com.net.wanjian.networkhospitalmanager.activity.MainActivity;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.bean.StudentApplyReserveReturn;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.consts.SharedPreferencesKeyConst;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.EncryptUtil;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.ToastUtil;

import java.util.LinkedHashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import io.reactivex.observers.DisposableObserver;

public class EditPasswordActivity extends BaseActivity {

    @BindView(R.id.top_back_text_tv)
    TextView topBackTextTv;
    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.top_title_tv)
    TextView topTitleTv;
    @BindView(R.id.edit_password_old_et)
    EditText editPasswordOldEt;
    @BindView(R.id.edit_password_new_et)
    EditText editPasswordNewEt;
    @BindView(R.id.edit_password_retry_new_et)
    EditText editPasswordRetryNewEt;
    @BindView(R.id.edit_password_submit_btn)
    Button editPasswordSubmitBtn;

    //        private final String pwRule = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";
    private final String pwRule = "[a-z0-9]";

    @Override
    protected void initData() {
        topTitleTv.setText("修改密码");
        topBackLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditPasswordActivity.this.finish();
            }
        });

        editPasswordSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入的密码
                String oldP = editPasswordOldEt.getText().toString();
                String newP = editPasswordNewEt.getText().toString();
                String rNewP = editPasswordRetryNewEt.getText().toString();

                if (oldP.length() < 6 || newP.length() < 6 || rNewP.length() < 6) {
                    ToastUtil.showToast("密码长度错误，请输入6位以上");
                    return;
                }

                if (rNewP.equals(newP)) {

                    submitEditPassword(oldP, rNewP);
                } else {
                    ToastUtil.showToast("两次输入密码不一致");
                }

            }
        });

    }

    private void submitEditPassword(String oldP, String rNewP) {
        //MD5加密密码
        SharedXmlUtil xmlUtil = SharedXmlUtil.getInstance();
        HttpUtil.saveModifyUserPassword(xmlUtil.getDeviceId(), xmlUtil.getToken(), xmlUtil.getHospitalId(), xmlUtil.getUserInfoId(), EncryptUtil.stringToMD5(oldP), EncryptUtil.stringToMD5(rNewP), new DisposableObserver<StudentApplyReserveReturn>() {
            @Override
            public void onNext(StudentApplyReserveReturn studentApplyReserveReturn) {

                Log.e(TAG, "onNext: " + studentApplyReserveReturn.getResult());
                if (studentApplyReserveReturn.getResult().equals(ErrorCodeReturn.success + "")) {
                    ToastUtil.showToast("修改密码成功，请重新登陆");
                    EditPasswordActivity.this.finish();
                    userCancel();
                } else {
                    if (studentApplyReserveReturn.getResult().equals("3001")) {
                        ToastUtil.showToast("旧密码错误");
                    }
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }

    private void userCancel() {
        JPushInterface.setAliasAndTags(this, "", new LinkedHashSet<String>(), new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {

            }
        });
        SharedXmlUtil.getInstance().delete(SharedPreferencesKeyConst.LOGIN_USER_NAME);
        SharedXmlUtil.getInstance().delete(SharedPreferencesKeyConst.LOGIN_USER_PASSWORD);
        openActivity(LoginActivity.class);
        finish();
        MainActivity.mainActivityInstance.finish();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_password;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
